<?php
class GoodsController extends Controller{
    private $db;

    public function __init(){
        //判断是否登陆
        $admin = Q('session.admin');
        if(empty($admin)){
            header('location:'.u('User/Login/Login'));
        }
        //获取商品类型
        $catelist = K("Cate") -> catelist() ;
        //指定函数
        $this -> db = k("Goods");

        $this -> assign('catelist',$catelist);
        $this -> assign('admin',$admin);
    }

    public function index(){
        //显示类别
        $type = Q('get.type',0);
        //获取商品列表
        $where ='';
        if(!empty($type)){
            $where = 'goods.cid = '.$type;
        }
        $gdata = $this -> db -> glist($where);
        $this -> assign('gdata',$gdata);
        $this -> assign('type',$type);
        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }
    //页面显示
    public function add(){
        $gid = Q('get.gid','');
        if(!empty($gid)){
            $where = 'gid = '.$gid;
            $data = $this -> db -> gone($where);
            $this ->assign('data',$data);
        }
        $this -> assign('gid',$gid);
        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }

    //修改和添加的操作
    public function modify(){
        $gid = Q('post.gid','');
        $Model = K('Goods');
        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        //实例化上传
        $upload = new upload('Upload/gimg/'.date("Y/m/d"));
        $uplofiles = $upload->upload();
        //判断是否为图片
        if(!empty($uplofiles)){
            $flieext = strtolower($uplofiles[0]['ext']);
            if($flieext !='jpg' && $flieext !='png'){
                $this -> error('请上传图片文件');
            }
        }
        //添加
        if(empty($gid)){
            $data = array(
                'gname' => $_POST['ocont'],
                'cid' => $_POST['cate'],
                'addtime' => time(),
                'reprice' => $_POST['money'],
                'v1price' => $_POST['v1m'],
                'v2price' => $_POST['v2m'],
                'gnotes' => $_POST['gnotes'],
            );
            if(!empty($uplofiles)){
                $data['gpic'] = $uplofiles[0]['path'];
            }
            if($this -> db -> addgoods($data)){
                $this -> success($message = '添加成功。',U('goods/index'), $time = 2);
                exit;
            }else{
                $this -> error('添加失败');
                exit;
            }
        }
        //修改
        if(!empty($gid)){
            $where = 'gid ='.$gid;
            $data = array(
                'gname' => $_POST['ocont'],
                'cid' => $_POST['cate'],
                'addtime' => time(),
                'reprice' => $_POST['money'],
                'v1price' => $_POST['v1m'],
                'v2price' => $_POST['v2m'],
                'notes' => $_POST['notes'],
            );
            if(!empty($uplofiles)){
                $data['gpic'] = $uplofiles[0]['path'];
            }

            if($this -> db -> upgoods($where,$data)){
                $this -> success($message = '修改成功。',U('goods/index'), $time = 2);
                exit;
            }else{
                $this -> error($Model -> error);
                exit;
            }
        }
    }
    //删除类型
    public function del(){
        $gid = Q('get.gid','');

        if(empty($gid)){
            header('location:'.u('cate/index'));
            exit;
        }
        $where = 'gid = '.$gid;
        if($this -> db -> goodsdel($where)){
            $this -> success($message = '删除成功。',U('goods/index'), $time = 2);
        }else{
            $this -> error('删除失败！');
            exit;
        }
    }
}